//
//  SLWelcomeController.swift
//  Sina15
//
//  Created by 盼 on 16/4/10.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class SLWelcomeController: UIViewController {
    
    //把welcome页面的view替换成UIImageView
    override func loadView() {
        
        view = UIImageView(image: UIImage(named: "ad_background"))

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        print("didLoad")

        setupUI()
    }
    
    override func viewWillLayoutSubviews() {
        
        print(view.frame)
    }
    
    override func viewDidLayoutSubviews() {
        print(view.frame)
    }
    
    override func viewDidAppear(animated: Bool) {

        startAnimation()
    }

    //设置UI界面
    func setupUI(){
        
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        //通过我们自己创建的分类，设置头像的圆角
       iconView.cornerRadius = 45
        
//        //通过layer裁剪图片
//        iconView.layer.cornerRadius = 45
//        iconView.layer.masksToBounds = true
        
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-180)
            make.size.equalTo(CGSize(width: 90, height: 90))

        }
        
        nameLabel.snp_makeConstraints(closure: { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        })
        
//  网络提供的数据不能使用，需要转换数据，为了简化多次洗数据      
//        iconView.sd_setImageWithURL(NSURL(string: (SLUserViewModel.sharedModel.userAccount?.avatar_large)!) , placeholderImage: UIImage(named: "avatar_default_big"))
        
        //直接通过viewModel的头像的URL属性加载头像
        iconView.sd_setImageWithURL(SLUserViewModel.sharedModel.headImageURL)
        
    }
    
    
    func startAnimation(){
        
        nameLabel.alpha = 0
        
        let offset = -screenHeight + 180
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
        }
        
        //使用动画方式刷新页面
        /*
        第三个参数：阻尼系数，阻尼系数越小，弹簧效果越大
        第四个参数：加速度，加速度越大，开始的速度就越大
        第五个参数：动画的可选项，OC中是按位与XXX｜XXX，swift按位与是多个枚举项写在数组中［］，如果是nil,可以直接给一个空数组
        */
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()

            }) { (_) -> Void in
                
            UIView.animateWithDuration(1.0, animations: { () -> Void in
                self.nameLabel.alpha = 1.0
                print("成功")
                }, completion: { (_) -> Void in
                    
            //dismiss 欢迎控制器之后发出改变根控制器的通知
            NSNotificationCenter.defaultCenter().postNotificationName(switchRootViewController, object: "Main")
                    
            })
        }
        
    }

    //懒加载控件
    private lazy var iconView:UIImageView = {
       
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        return icon
    }()
    
//    private lazy var nameLabel: UILabel = {
//       
//        let label = UILabel()
//        label.font = UIFont.systemFontOfSize(14)
//        label.text = SLUserViewModel.sharedModel.userAccount?.name
//        
//        label.sizeToFit()
//        return label
//    }()
    
    private lazy var nameLabel = UILabel(labelText: (SLUserViewModel.sharedModel.userAccount?.name)!, color: UIColor.darkGrayColor(), fontsize: 14)
}
